//
//  VideoListViewController.m
//  Shanghai Cuisine
//
//  Created by iOS Two on 14/12/16.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "VideoListViewController.h"
#import "VideoViewController.h"

@interface VideoListViewController ()

@property (strong,nonatomic) NSIndexPath *index;

@end

@implementation VideoListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatController];
    [self getData];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 30)];
    titleLabel.text = @"视频";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.textColor = [UIColor colorWithRed:0.97 green:0.69 blue:0.17 alpha:1];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.97 green:0.69 blue:0.17 alpha:1];
    self.navigationItem.titleView = titleLabel;
    // Do any additional setup after loading the view.
}
-(void)creatController {
    _mainTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kwidth, kHeight - 64) style:UITableViewStylePlain];
    _mainTable.delegate = self;
    _mainTable.dataSource = self;
    _mainTable.rowHeight = 90;
    _mainTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    _mainTable.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    [self.view addSubview:_mainTable];
}
-(void)getData {
    _videoLink = [[NSMutableArray alloc]init];
    _videoName = [[NSMutableArray alloc]init];
    _videoCover  = [[NSMutableArray alloc]init];
    AVQuery *query = [[AVQuery alloc]initWithClassName:@"video"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        for (int i = 0; i < objects.count; i++) {
            [_videoName addObject:[[objects objectAtIndex:i] objectForKey:@"name"]];
            [_videoLink addObject:[[objects objectAtIndex:i] objectForKey:@"link"]];
            AVFile *file = [[objects objectAtIndex:i] objectForKey:@"cover"];
            [_videoCover addObject:file.url];
        }
        [_mainTable reloadData];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _videoName.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    } else {
        for (UIView *view in [cell.contentView subviews]) {
            [view removeFromSuperview];
        }
    }
    cell.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 120, 69)];
    [image setImageURLStr:[_videoCover objectAtIndex:indexPath.row] placeholder:[UIImage imageNamed:@"11_08"]];
    [cell.contentView addSubview:image];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(138, 35, 200, 20)];
    label.text = [_videoName objectAtIndex:indexPath.row];
    label.textColor = [UIColor colorWithRed:0.33 green:0.27 blue:0.23 alpha:1];
    label.font = [UIFont systemFontOfSize:15];
    [cell.contentView addSubview:label];
    
    UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(5, 89, kwidth - 5, 0.5)];
    line.backgroundColor = [UIColor colorWithRed:0.82 green:0.82 blue:0.82 alpha:1];
    [cell.contentView addSubview:line];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _index = indexPath;
    if ([[self netWorkType] isEqualToString:@"wifi"]) {
        VideoViewController *video = [[VideoViewController alloc]init];
        video.videoLink = [_videoLink objectAtIndex:indexPath.row];
        video.videoName = [_videoName objectAtIndex:indexPath.row];
        self.navigationItem.title = @"返回";
        [self.navigationController pushViewController:video animated:YES];
    } else if ([[self netWorkType] isEqualToString:@"gprs"]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"当前网络不适合观看视频,是否继续?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    } else if ([[self netWorkType] isEqualToString:@"nil"]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"当前网络不给力,请稍后再试试" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        VideoViewController *video = [[VideoViewController alloc]init];
        video.videoLink = [_videoLink objectAtIndex:_index.row];
        video.videoName = [_videoName objectAtIndex:_index.row];
        self.navigationItem.title = @"返回";
        [self.navigationController pushViewController:video animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
